﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using WCS.Infrastructure.Attr;
using WCS.Infrastructure.Dto.Request.Organization;
using WCS.Infrastructure.Dto.Request.Payment;
using WCS.Service.Instance;
using WCS.Service.Interface;

namespace WCS.Api.Admin.Controllers
{
    /// <summary>
    /// 收款码管理
    /// </summary>
    public class PaymentCodeController : BaseController
    {
        private readonly IConfiguration _configuration;
        private readonly IPaymentCodeService _paymentCodeService;

        public PaymentCodeController(IConfiguration configuration, IPaymentCodeService paymentCodeService, IUserService userService) : base(userService)
        {
            _configuration = configuration;
            _paymentCodeService = paymentCodeService;
        }

        /// <summary>
        /// 新增收款码【CodeName，StoreId，(BindUserId，DividedAgreementId，SettlementCycle)】
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "新增收款码", Group = "收款码模块")]
        public async Task<IActionResult> AddPaymentCode(PaymentCodeRequestDto dto)
        {
            var result = await _paymentCodeService.AddPaymentCodeAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 编辑收款码【CodeName，StoreId,DividedAgreementId，SettlementCycle】
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "编辑收款码", Group = "收款码模块")]
        public async Task<IActionResult> UpdatePaymentCode(PaymentCodeRequestDto dto)
        {
            var result = await _paymentCodeService.UpdatePaymentCodeAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 获取收款码列表
        /// </summary>
        /// <param name="page">页数</param>
        /// <param name="limit">每页条数</param>
        /// <param name="key">查询条件【根据收款码Code】</param>
        /// <param name="storeId">门店ID</param>
        /// <param name="merchantId">商户ID</param>
        /// <param name="queryNoDividedAgreement">是否查询无分账协议的收款码（true=查询分账协议为空的，false=查询分账协议不为空的，null=不限制）</param>
        [HttpGet]
        public async Task<IActionResult> GetPaymentCodeList(int page = 1, int limit = 10, string? key = "", string? storeId = "", string? merchantId = "", bool? queryNoDividedAgreement = null)
        {
            var result = await _paymentCodeService.GetPaymentCodeListAsync(new PaymentCodeRequestDto
            {
                Page = page,
                Limit = limit,
                Key = key,
                StoreId = storeId,
                MerchantId = merchantId,
                QueryNoDividedAgreement = queryNoDividedAgreement
            });
            return Ok(result);
        }

        /// <summary>
        /// 获取单个收款码详情
        /// </summary>
        /// <param name="id"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetPaymentCodeDetail(string? id = "", string? code = "")
        {
            var result = await _paymentCodeService.GetPaymentCodeDetailAsync(new PaymentCodeRequestDto
            {
                Id = id,
                Code = code
            });
            return Ok(result);
        }

        /// <summary>
        /// 在收款码详情里批量设置关联成员
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "在收款码详情里批量设置关联成员", Group = "收款码模块")]
        public async Task<IActionResult> SetPaymentCodeMembers(BindMembersRequestDto dto)
        {
            var result = await _paymentCodeService.SetPaymentCodeMembersAsync(dto);
            return Ok(result);
        }

    }
}
